home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / viewkit / xcontact / parody / test.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.1 KB  |  120 lines

  1.  
  2. #include "parody.h"
  3. #include <stdlib.h>
  4.  
  5. enum ClassIdent {
  6.   FIELDS_SETUP=123,
  7.   ATTRIBUTE_SETUP,
  8.   CARD_INFO
  9. };
  10.  
  11. Parody *theDatabase = NULL;
  12.  
  13.  
  14. // Generic Integer Key.
  15.  
  16. class IntKey : public Key {
  17.   int _key;
  18.  
  19.   int operator>( Key &key )    { return _key>((IntKey &) key).key(); }
  20.   int operator==( Key &key )    { return _key==((IntKey &) key).key(); }
  21.  
  22.   Key& operator=( Key &key )    {
  23.     if ( this != &key ) {
  24.       Key::operator=(key);
  25.       IntKey& intKey = (IntKey&) key;
  26.       _key = intKey._key;
  27.     }
  28.     return *this;
  29.   }
  30.  
  31.  
  32.   void Write( fstream& ndx )    { cerr << "(" << (long)this << ")"<<"IntKey::Write: "<<_key <<endl; ndx.write( (char *) &_key, sizeof _key ); }
  33.   void Read( fstream& ndx )    { cerr << "IntKey::Read: "<<_key <<endl;ndx.read( (char *) &_key, sizeof _key ); }
  34.  
  35.   Key *Make()            { return new IntKey(); }
  36.   pBool isNullValue()        { return (pBool) ( _key==0 ); }
  37.  
  38.  public:
  39.   IntKey( int key=0 )        { cerr << "(" << (long)this << ")"<<"IntKey::IntKey("<<key<<")\n";_key = key; }
  40.   int key() const        { return _key; }
  41. };
  42.  
  43.  
  44. class C : public Persistent {
  45.  
  46.  private:
  47.   IntKey _key;
  48.  
  49.   char* _s;
  50.  
  51.  protected:
  52.   void Read();
  53.   void Write();
  54.  
  55.  public:
  56.   C( int id=0 );
  57.   ~C();
  58.  
  59.   const char* s() { return _s; }
  60.   const char* s( const char* news ) { _s = strdup( news ); return _s; }
  61.  
  62. };
  63.  
  64.  
  65. C::C( int id ) : Persistent( *theDatabase, FIELDS_SETUP ), _key( id )
  66. {
  67.   _s = strdup( "Nothing" );
  68.   cerr <<"C::C:_key = "<<_key.key()<<endl;
  69.   LoadObject();
  70. }
  71.  
  72. C::~C()
  73. {
  74.   cerr << "SaveObject\n";
  75.  
  76.   SaveObject();
  77.   cerr << "C::~C:_key: " << _key.key() << endl;
  78.   delete _s;
  79. }
  80.  
  81. void
  82. C::Read()
  83. {
  84.   pString ss;
  85.   ReadObject( ss );
  86.   delete _s;
  87.   _s = strdup( ss );
  88.   cerr << "C::Read: " << _s << "\n";
  89. }
  90.  
  91. void
  92. C::Write()
  93. {
  94.   cerr << "C::Writing... " << _s << "\n";
  95.   WriteObject( pString( _s ) );
  96. }
  97.  
  98.  
  99. main( int argc, char** argv )
  100. {
  101.   theDatabase = new Parody( "FOO" );
  102.  
  103.   C *c = new C( 5 );
  104.  
  105.   if ( c->ObjectExists() ) {
  106.     cerr << "Content: " << c->s() << endl;
  107.   } else {
  108.     cerr << "Adding object...\n";
  109.     c->AddObject();
  110.   }
  111.  
  112.   if ( argc==2 ) {
  113.     c->s( argv[ 1 ] );
  114.     c->ChangeObject();
  115.   }
  116.  
  117.   delete c;
  118.   delete theDatabase;
  119. }
  120.